home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gawk / gawk2-~1.zoo / gawk_st / gawk.man < prev    next >
Encoding:
Text File  |  1994-05-23  |  46.0 KB  |  1,146 lines

  1.  
  2.  
  3.  
  4. GAWK(1)                 Utility Commands                  GAWK(1)
  5.  
  6.  
  7.  
  8. NAME
  9.      gawk - pattern scanning and processing language
  10.  
  11. SYNOPSIS
  12.      gawk [ POSIX or GNU style options ] -f program-file [ -- ] file ...
  13.      gawk [ POSIX or GNU style options ] [ -- ] program-text file ...
  14.  
  15. DESCRIPTION
  16.      Gawk is the GNU Project's implementation of the AWK program-
  17.      ming language.  It conforms to the definition of the
  18.      language in the POSIX 1003.2 Command Language And Utilities
  19.      Standard.  This version in turn is based on the description
  20.      in The AWK Programming Language, by Aho, Kernighan, and
  21.      Weinberger, with the additional features defined in the Sys-
  22.      tem V Release 4 version of UNIX awk.  Gawk also provides
  23.      some GNU-specific extensions.
  24.  
  25.      The command line consists of options to gawk itself, the AWK
  26.      program text (if not supplied via the -f or --file options),
  27.      and values to be made available in the ARGC and ARGV pre-
  28.      defined AWK variables.
  29.  
  30. OPTIONS
  31.      Gawk options may be either the traditional POSIX one letter
  32.      options, or the GNU style long options.  POSIX style options
  33.      start with a single ``-'', while GNU long options start with
  34.      ``--''.  GNU style long options are provided for both GNU-
  35.      specific features and for POSIX mandated features.  Other
  36.      implementations of the AWK language are likely to only
  37.      accept the traditional one letter options.
  38.  
  39.      Following the POSIX standard, gawk-specific options are sup-
  40.      plied via arguments to the -W option.  Multiple -W options
  41.      may be supplied, or multiple arguments may be supplied
  42.      together if they are separated by commas, or enclosed in
  43.      quotes and separated by white space.  Case is ignored in
  44.      arguments to the -W option.  Each -W option has a
  45.      corresponding GNU style long option, as detailed below.
  46.      Arguments to GNU style long options are either joined with
  47.      the option by an = sign, with no intervening spaces, or they
  48.      may be provided in the next command line argument.
  49.  
  50.      Gawk accepts the following options.
  51.  
  52.      -F fs
  53.      --field-separator=fs
  54.           Use fs for the input field separator (the value of the
  55.           FS predefined variable).
  56.  
  57.      -v var=val
  58.      --assign=var=val
  59.           Assign the value val, to the variable var, before exe-
  60.           cution of the program begins.  Such variable values are
  61.           available to the BEGIN block of an AWK program.
  62.  
  63.      -f program-file
  64.      --file=program-file
  65.           Read the AWK program source from the file program-file,
  66.           instead of from the first command line argument.  Mul-
  67.           tiple -f (or --file) options may be used.
  68.  
  69.      -mf=NNN
  70.      -mr=NNN
  71.           Set various memory limits to the value NNN.  The f flag
  72.           sets the maximum number of fields, and the r flag sets
  73.           the maximum record size.  These two flags and the -m
  74.           option are from the AT&T Bell Labs research version of
  75.           UNIX awk.  They are ignored by gawk, since gawk has no
  76.           pre-defined limits.
  77.      -W compat
  78.      --compat    Run in compatibility mode.  In compatibility
  79.                  mode, gawk behaves identically to UNIX awk; none
  80.                  of the GNU-specific extensions are recognized.
  81.                  See GNU EXTENSIONS, below, for more information.
  82.  
  83.      -W copyleft
  84.      -W copyright
  85.      --copyleft
  86.      --copyright Print the short version of the GNU copyright
  87.                  information message on the error output.
  88.  
  89.      -W help
  90.      -W usage
  91.      --help
  92.      --usage     Print a relatively short summary of the avail-
  93.                  able options on the error output.  Per the GNU
  94.                  Coding Standards, these options cause an immedi-
  95.                  ate, successful exit.
  96.  
  97.      -W lint
  98.      --lint      Provide warnings about constructs that are dubi-
  99.                  ous or non-portable to other AWK implementa-
  100.                  tions.
  101.      -W posix
  102.      --posix     This turns on compatibility mode, with the fol-
  103.                  lowing additional restrictions:
  104.  
  105.                  + \x escape sequences are not recognized.
  106.  
  107.                  + The synonym func for the keyword function is
  108.                    not recognized.
  109.  
  110.                  + The operators ** and **= cannot be used in
  111.                    place of ^ and ^=.
  112.  
  113.      -W source=program-text
  114.      --source=program-text
  115.                  Use program-text as AWK program source code.
  116.                  This option allows the easy intermixing of
  117.                  library functions (used via the -f and --file
  118.                  options) with source code entered on the command
  119.                  line.  It is intended primarily for medium to
  120.                  large size AWK programs used in shell scripts.
  121.                  The -W source= form of this option uses the rest
  122.                  of the command line argument for program-text;
  123.                  no other options to -W will be recognized in the
  124.                  same argument.
  125.  
  126.      -W version
  127.      --version   Print version information for this particular
  128.                  copy of gawk on the error output.  This is use-
  129.                  ful mainly for knowing if the current copy of
  130.                  gawk on your system is up to date with respect
  131.                  to whatever the Free Software Foundation is dis-
  132.                  tributing.  Per the GNU Coding Standards, these
  133.                  options cause an immediate, successful exit.
  134.  
  135.      --          Signal the end of options. This is useful to
  136.                  allow further arguments to the AWK program
  137.                  itself to start with a ``-''.  This is mainly
  138.                  for consistency with the argument parsing con-
  139.                  vention used by most other POSIX programs.
  140.  
  141.      In compatibility mode, any other options are flagged as
  142.      illegal, but are otherwise ignored.  In normal operation, as
  143.      long as program text has been supplied, unknown options are
  144.      passed on to the AWK program in the ARGV array for process-
  145.      ing.  This is particularly useful for running AWK programs
  146.      via the ``#!'' executable interpreter mechanism.
  147.  
  148. AWK PROGRAM EXECUTION
  149.      An AWK program consists of a sequence of pattern-action
  150.      statements and optional function definitions.
  151.  
  152.           pattern   { action statements }
  153.           function name(parameter list) { statements }
  154.  
  155.      Gawk first reads the program source from the program-file(s)
  156.      if specified, from arguments to -W source=, or from the
  157.      first non-option argument on the command line.  The -f and
  158.      -W source= options may be used multiple times on the command
  159.      line.  Gawk will read the program text as if all the
  160.      program-files and command line source texts had been con-
  161.      catenated together.  This is useful for building libraries
  162.      of AWK functions, without having to include them in each new
  163.      AWK program that uses them.  It also provides the ability to
  164.      mix library functions with command line programs.
  165.  
  166.      The environment variable AWKPATH specifies a search path to
  167.      use when finding source files named with the -f option.  If
  168.      this variable does not exist, the default path on Atari is
  169.      ".,c:\lib\awk,c:\gnu\lib\awk".  When giving path as a string
  170.      to gawk ``\'' path separator has to be escaped with itself to
  171.      prevent its misinterpretation.  gawk compiled with gnu.olb
  172.      libraries recognized both ``/'' and ``\'' characters as path
  173.      separators.  If a file name given to the -f option contains
  174.      a path separator character, no path search is performed.
  175.  
  176.      Gawk executes AWK programs in the following order.  First,
  177.      all variable assignments specified via the -v option are
  178.      performed.  Next, gawk compiles the program into an internal
  179.      form.  Then, gawk executes the code in the BEGIN block(s)
  180.      (if any), and then proceeds to read each file named in the
  181.      ARGV array.  If there are no files named on the command
  182.      line, gawk reads the standard input.
  183.  
  184.      If a filename on the command line has the form var=val it is
  185.      treated as a variable assignment. The variable var will be
  186.      assigned the value val.  (This happens after any BEGIN
  187.      block(s) have been run.) Command line variable assignment is
  188.      most useful for dynamically assigning values to the vari-
  189.      ables AWK uses to control how input is broken into fields
  190.      and records. It is also useful for controlling state if mul-
  191.      tiple passes are needed over a single data file.
  192.  
  193.      If the value of a particular element of ARGV is empty (""),
  194.      gawk skips over it.
  195.  
  196.      For each line in the input, gawk tests to see if it matches
  197.      any pattern in the AWK program.  For each pattern that the
  198.      line matches, the associated action is executed.  The pat-
  199.      terns are tested in the order they occur in the program.
  200.  
  201.      Finally, after all the input is exhausted, gawk executes the
  202.      code in the END block(s) (if any).
  203.  
  204. VARIABLES AND FIELDS
  205.      AWK variables are dynamic; they come into existence when
  206.      they are first used. Their values are either floating-point
  207.      numbers or strings, or both, depending upon how they are
  208.      used. AWK also has one dimensional arrays; arrays with mul-
  209.      tiple dimensions may be simulated.  Several pre-defined
  210.      variables are set as a program runs; these will be described
  211.      as needed and summarized below.
  212.  
  213.      Fields
  214.  
  215.      As each input line is read, gawk splits the line into
  216.      fields, using the value of the FS variable as the field
  217.      separator.  If FS is a single character, fields are
  218.      separated by that character.  Otherwise, FS is expected to
  219.      be a full regular expression.  In the special case that FS
  220.      is a single blank, fields are separated by runs of blanks
  221.      and/or tabs.  Note that the value of IGNORECASE (see below)
  222.      will also affect how fields are split when FS is a regular
  223.      expression.
  224.  
  225.      If the FIELDWIDTHS variable is set to a space separated list
  226.      of numbers, each field is expected to have fixed width, and
  227.      gawk will split up the record using the specified widths.
  228.      The value of FS is ignored.  Assigning a new value to FS
  229.      overrides the use of FIELDWIDTHS, and restores the default
  230.      behavior.
  231.  
  232.      Each field in the input line may be referenced by its posi-
  233.      tion, $1, $2, and so on.  $0 is the whole line. The value of
  234.      a field may be assigned to as well.  Fields need not be
  235.      referenced by constants:
  236.  
  237.           n = 5
  238.           print $n
  239.  
  240.      prints the fifth field in the input line.  The variable NF
  241.      is set to the total number of fields in the input line.
  242.  
  243.      References to non-existent fields (i.e. fields after $NF)
  244.      produce the null-string. However, assigning to a non-
  245.      existent field (e.g., $(NF+2) = 5) will increase the value
  246.      of NF, create any intervening fields with the null string as
  247.      their value, and cause the value of $0 to be recomputed,
  248.      with the fields being separated by the value of OFS.  Refer-
  249.      ences to negative numbered fields cause a fatal error.
  250.  
  251.      Built-in Variables
  252.  
  253.      AWK's built-in variables are:
  254.  
  255.      ARGC        The number of command line arguments (does not
  256.                  include options to gawk, or the program source).
  257.  
  258.      ARGIND      The index in ARGV of the current file being
  259.                  processed.
  260.  
  261.      ARGV        Array of command line arguments. The array is
  262.                  indexed from 0 to ARGC - 1.  Dynamically chang-
  263.                  ing the contents of ARGV can control the files
  264.                  used for data.
  265.  
  266.      CONVFMT     The conversion format for numbers, "%.6g", by
  267.                  default.
  268.  
  269.      ENVIRON     An array containing the values of the current
  270.                  environment.  The array is indexed by the
  271.                  environment variables, each element being the
  272.                  value of that variable (e.g., ENVIRON["HOME"]
  273.                  might be /u/arnold).  Changing this array does
  274.                  not affect the environment seen by programs
  275.                  which gawk spawns via redirection or the sys-
  276.                  tem() function.  (This may change in a future
  277.                  version of gawk.)
  278.  
  279.      ERRNO       If a system error occurs either doing a redirec-
  280.                  tion for getline, during a read for getline, or
  281.                  during a close(), then ERRNO will contain a
  282.                  string describing the error.
  283.  
  284.      FIELDWIDTHS A white-space separated list of fieldwidths.
  285.                  When set, gawk parses the input into fields of
  286.                  fixed width, instead of using the value of the
  287.                  FS variable as the field separator.  The fixed
  288.                  field width facility is still experimental;
  289.                  expect the semantics to change as gawk evolves
  290.                  over time.
  291.  
  292.      FILENAME    The name of the current input file.  If no files
  293.                  are specified on the command line, the value of
  294.                  FILENAME is ``-''.  However, FILENAME is unde-
  295.                  fined inside the BEGIN block.
  296.  
  297.      FNR         The input record number in the current input
  298.                  file.
  299.  
  300.      FS          The input field separator, a blank by default.
  301.  
  302.      IGNORECASE  Controls the case-sensitivity of all regular
  303.                  expression operations. If IGNORECASE has a non-
  304.                  zero value, then pattern matching in rules,
  305.                  field splitting with FS, regular expression
  306.                  matching with ~ and !~, and the gsub(), index(),
  307.                  match(), split(), and sub() pre-defined func-
  308.                  tions will all ignore case when doing regular
  309.                  expression operations.  Thus, if IGNORECASE is
  310.                  not equal to zero, /aB/ matches all of the
  311.                  strings "ab", "aB", "Ab", and "AB".  As with all
  312.                  AWK variables, the initial value of IGNORECASE
  313.                  is zero, so all regular expression operations
  314.                  are normally case-sensitive.
  315.  
  316.      NF          The number of fields in the current input
  317.                  record.
  318.  
  319.      NR          The total number of input records seen so far.
  320.  
  321.      OFMT        The output format for numbers, "%.6g", by
  322.                  default.
  323.  
  324.      OFS         The output field separator, a blank by default.
  325.  
  326.      ORS         The output record separator, by default a new-
  327.                  line.
  328.  
  329.      RS          The input record separator, by default a new-
  330.                  line.  RS is exceptional in that only the first
  331.                  character of its string value is used for
  332.                  separating records.  (This will probably change
  333.                  in a future release of gawk.) If RS is set to
  334.                  the null string, then records are separated by
  335.                  blank lines.  When RS is set to the null string,
  336.                  then the newline character always acts as a
  337.                  field separator, in addition to whatever value
  338.                  FS may have.
  339.  
  340.      RSTART      The index of the first character matched by
  341.                  match(); 0 if no match.
  342.  
  343.      RLENGTH     The length of the string matched by match(); -1
  344.                  if no match.
  345.  
  346.      SUBSEP      The character used to separate multiple sub-
  347.                  scripts in array elements, by default "\034".
  348.  
  349.      Arrays
  350.  
  351.      Arrays are subscripted with an expression between square
  352.      brackets ([ and ]).  If the expression is an expression list
  353.      (expr, expr ...) then the array subscript is a string con-
  354.      sisting of the concatenation of the (string) value of each
  355.      expression, separated by the value of the SUBSEP variable.
  356.      This facility is used to simulate multiply dimensioned
  357.      arrays. For example:
  358.  
  359.           i = "A" ; j = "B" ; k = "C"
  360.           x[i, j, k] = "hello, world\n"
  361.  
  362.      assigns the string "hello, world\n" to the element of the
  363.      array x which is indexed by the string "A\034B\034C". All
  364.      arrays in AWK are associative, i.e. indexed by string
  365.      values.
  366.  
  367.      The special operator in may be used in an if or while state-
  368.      ment to see if an array has an index consisting of a partic-
  369.      ular value.
  370.  
  371.           if (val in array)
  372.                print array[val]
  373.  
  374.      If the array has multiple subscripts, use (i, j) in array.
  375.  
  376.      The in construct may also be used in a for loop to iterate
  377.      over all the elements of an array.
  378.  
  379.      An element may be deleted from an array using the delete
  380.      statement.  The delete statement may also be used to delete
  381.      the entire contents of an array.
  382.  
  383.      Variable Typing And Conversion
  384.  
  385.      Variables and fields may be (floating point) numbers, or
  386.      strings, or both. How the value of a variable is interpreted
  387.      depends upon its context. If used in a numeric expression,
  388.      it will be treated as a number, if used as a string it will
  389.      be treated as a string.
  390.  
  391.      To force a variable to be treated as a number, add 0 to it;
  392.      to force it to be treated as a string, concatenate it with
  393.      the null string.
  394.  
  395.      When a string must be converted to a number, the conversion
  396.      is accomplished using atof(3).  A number is converted to a
  397.      string by using the value of CONVFMT as a format string for
  398.      sprintf(3), with the numeric value of the variable as the
  399.      argument.  However, even though all numbers in AWK are
  400.      floating-point, integral values are always converted as
  401.      integers.  Thus, given
  402.  
  403.           CONVFMT = "%2.2f"
  404.           a = 12
  405.           b = a ""
  406.  
  407.      the variable b has a string value of "12" and not "12.00".
  408.  
  409.      Gawk performs comparisons as follows: If two variables are
  410.      numeric, they are compared numerically.  If one value is
  411.      numeric and the other has a string value that is a ``numeric
  412.      string,'' then comparisons are also done numerically.  Oth-
  413.      erwise, the numeric value is converted to a string and a
  414.      string comparison is performed.  Two strings are compared,
  415.      of course, as strings.  According to the POSIX standard,
  416.      even if two strings are numeric strings, a numeric com-
  417.      parison is performed.  However, this is clearly incorrect,
  418.      and gawk does not do this.
  419.  
  420.      Uninitialized variables have the numeric value 0 and the
  421.      string value "" (the null, or empty, string).
  422.  
  423. PATTERNS AND ACTIONS
  424.      AWK is a line oriented language. The pattern comes first,
  425.      and then the action. Action statements are enclosed in { and
  426.      }.  Either the pattern may be missing, or the action may be
  427.      missing, but, of course, not both. If the pattern is miss-
  428.      ing, the action will be executed for every single line of
  429.      input.  A missing action is equivalent to
  430.  
  431.           { print }
  432.  
  433.      which prints the entire line.
  434.  
  435.      Comments begin with the ``#'' character, and continue until
  436.      the end of the line.  Blank lines may be used to separate
  437.      statements.  Normally, a statement ends with a newline, how-
  438.      ever, this is not the case for lines ending in a ``,'',
  439.      ``{'', ``?'', ``:'', ``&&'', or ``||''.  Lines ending in do
  440.      or else also have their statements automatically continued
  441.      on the following line.  In other cases, a line can be con-
  442.      tinued by ending it with a ``\'', in which case the newline
  443.      will be ignored.
  444.  
  445.      Multiple statements may be put on one line by separating
  446.      them with a ``;''.  This applies to both the statements
  447.      within the action part of a pattern-action pair (the usual
  448.      case), and to the pattern-action statements themselves.
  449.  
  450.      Patterns
  451.      AWK patterns may be one of the following:
  452.  
  453.           BEGIN
  454.           END
  455.           /regular expression/
  456.           relational expression
  457.           pattern && pattern
  458.           pattern || pattern
  459.           pattern ? pattern : pattern
  460.           (pattern)
  461.           ! pattern
  462.           pattern1, pattern2
  463.  
  464.      BEGIN and END are two special kinds of patterns which are
  465.      not tested against the input.  The action parts of all BEGIN
  466.      patterns are merged as if all the statements had been writ-
  467.      ten in a single BEGIN block. They are executed before any of
  468.      the input is read. Similarly, all the END blocks are merged,
  469.      and executed when all the input is exhausted (or when an
  470.      exit statement is executed).  BEGIN and END patterns cannot
  471.      be combined with other patterns in pattern expressions.
  472.      BEGIN and END patterns cannot have missing action parts.
  473.  
  474.      For /regular expression/ patterns, the associated statement
  475.      is executed for each input line that matches the regular
  476.      expression.  Regular expressions are the same as those in
  477.      egrep(1), and are summarized below.
  478.  
  479.      A relational expression may use any of the operators defined
  480.      below in the section on actions.  These generally test
  481.      whether certain fields match certain regular expressions.
  482.  
  483.      The &&, ||, and ! operators are logical AND, logical OR, and
  484.      logical NOT, respectively, as in C.  They do short-circuit
  485.      evaluation, also as in C, and are used for combining more
  486.      primitive pattern expressions. As in most languages,
  487.      parentheses may be used to change the order of evaluation.
  488.  
  489.      The ?: operator is like the same operator in C. If the first
  490.      pattern is true then the pattern used for testing is the
  491.      second pattern, otherwise it is the third. Only one of the
  492.      second and third patterns is evaluated.
  493.  
  494.      The pattern1, pattern2 form of an expression is called a
  495.      range pattern.  It matches all input records starting with a
  496.      line that matches pattern1, and continuing until a record
  497.      that matches pattern2, inclusive. It does not combine with
  498.      any other sort of pattern expression.
  499.  
  500.      Regular Expressions
  501.      Regular expressions are the extended kind found in egrep.
  502.      They are composed of characters as follows:
  503.  
  504.      c          matches the non-metacharacter c.
  505.  
  506.      \c         matches the literal character c.
  507.  
  508.      .          matches any character except newline.
  509.  
  510.      ^          matches the beginning of a line or a string.
  511.  
  512.      $          matches the end of a line or a string.
  513.  
  514.      [abc...]   character class, matches any of the characters
  515.                 abc....
  516.  
  517.      [^abc...]  negated character class, matches any character
  518.                 except abc... and newline.
  519.  
  520.      r1|r2      alternation: matches either r1 or r2.
  521.  
  522.      r1r2       concatenation: matches r1, and then r2.
  523.  
  524.      r+         matches one or more r's.
  525.  
  526.      r*         matches zero or more r's.
  527.  
  528.      r?         matches zero or one r's.
  529.  
  530.      (r)        grouping: matches r.
  531.  
  532.      The escape sequences that are valid in string constants (see
  533.      below) are also legal in regular expressions.
  534.  
  535.      Actions
  536.      Action statements are enclosed in braces, { and }.  Action
  537.      statements consist of the usual assignment, conditional, and
  538.      looping statements found in most languages. The operators,
  539.      control statements, and input/output statements available
  540.      are patterned after those in C.
  541.  
  542.      Operators
  543.  
  544.      The operators in AWK, in order of increasing precedence, are
  545.  
  546.      = += -=
  547.      *= /= %= ^= Assignment. Both absolute assignment (var =
  548.                  value) and operator-assignment (the other forms)
  549.                  are supported.
  550.  
  551.      ?:          The C conditional expression. This has the form
  552.                  expr1 ? expr2 : expr3. If expr1 is true, the
  553.                  value of the expression is expr2, otherwise it
  554.                  is expr3.  Only one of expr2 and expr3 is
  555.                  evaluated.
  556.  
  557.      ||          Logical OR.
  558.  
  559.      &&          Logical AND.
  560.  
  561.      ~ !~        Regular expression match, negated match.  NOTE:
  562.                  Do not use a constant regular expression (/foo/)
  563.                  on the left-hand side of a ~ or !~.  Only use
  564.                  one on the right-hand side.  The expression
  565.                  /foo/ ~ exp has the same meaning as (($0 ~
  566.                  /foo/) ~ exp).  This is usually not what was
  567.                  intended.
  568.  
  569.      < >
  570.      <= >=
  571.      != ==       The regular relational operators.
  572.  
  573.      blank       String concatenation.
  574.  
  575.      + -         Addition and subtraction.
  576.  
  577.      * / %       Multiplication, division, and modulus.
  578.  
  579.      + - !       Unary plus, unary minus, and logical negation.
  580.  
  581.      ^           Exponentiation (** may also be used, and **= for
  582.                  the assignment operator).
  583.  
  584.      ++ --       Increment and decrement, both prefix and post-
  585.                  fix.
  586.  
  587.      $           Field reference.
  588.  
  589.      Control Statements
  590.  
  591.      The control statements are as follows:
  592.  
  593.           if (condition) statement [ else statement ]
  594.           while (condition) statement
  595.           do statement while (condition)
  596.           for (expr1; expr2; expr3) statement
  597.           for (var in array) statement
  598.           break
  599.           continue
  600.           delete array[index]
  601.           delete array
  602.           exit [ expression ]
  603.           { statements }
  604.  
  605.      I/O Statements
  606.  
  607.      The input/output statements are as follows:
  608.  
  609.      close(filename)       Close file (or pipe, see below).
  610.  
  611.      getline               Set $0 from next input record; set NF,
  612.                            NR, FNR.
  613.  
  614.      getline <file         Set $0 from next record of file; set
  615.                            NF.
  616.  
  617.      getline var           Set var from next input record; set
  618.                            NF, FNR.
  619.  
  620.      getline var <file     Set var from next record of file.
  621.  
  622.      next                  Stop processing the current input
  623.                            record. The next input record is read
  624.                            and processing starts over with the
  625.                            first pattern in the AWK program. If
  626.                            the end of the input data is reached,
  627.                            the END block(s), if any, are exe-
  628.                            cuted.
  629.  
  630.      next file             Stop processing the current input
  631.                            file.  The next input record read
  632.                            comes from the next input file.
  633.                            FILENAME is updated, FNR is reset to
  634.                            1, and processing starts over with the
  635.                            first pattern in the AWK program. If
  636.                            the end of the input data is reached,
  637.                            the END block(s), if any, are exe-
  638.                            cuted.
  639.  
  640.      print                 Prints the current record.
  641.  
  642.      print expr-list       Prints expressions.  Each expression
  643.                            is separated by the value of the OFS
  644.                            variable. The output record is ter-
  645.                            minated with the value of the ORS
  646.                            variable.
  647.  
  648.      print expr-list >file Prints expressions on file.  Each
  649.                            expression is separated by the value
  650.                            of the OFS variable. The output record
  651.                            is terminated with the value of the
  652.                            ORS variable.
  653.  
  654.      printf fmt, expr-list Format and print.
  655.  
  656.      printf fmt, expr-list >file
  657.                            Format and print on file.
  658.  
  659.      system(cmd-line)      Execute the command cmd-line, and
  660.                            return the exit status.  (This may not
  661.                            be available on non-POSIX systems.)
  662.  
  663.      Other input/output redirections are also allowed. For print
  664.      and printf, >>file appends output to the file, while | com-
  665.      mand writes on a pipe.  In a similar fashion, command | get-
  666.      line pipes into getline.  The getline command will return 0
  667.      on end of file, and -1 on an error.
  668.  
  669.      The printf Statement
  670.  
  671.      The AWK versions of the printf statement and sprintf() func-
  672.      tion (see below) accept the following conversion specifica-
  673.      tion formats:
  674.  
  675.      %c   An ASCII character.  If the argument used for %c is
  676.           numeric, it is treated as a character and printed.
  677.           Otherwise, the argument is assumed to be a string, and
  678.           the only first character of that string is printed.
  679.  
  680.      %d   A decimal number (the integer part).
  681.  
  682.      %i   Just like %d.
  683.  
  684.      %e   A floating point number of the form [-]d.ddddddE[+-]dd.
  685.  
  686.      %f   A floating point number of the form [-]ddd.dddddd.
  687.  
  688.      %g   Use e or f conversion, whichever is shorter, with non-
  689.           significant zeros suppressed.
  690.  
  691.      %o   An unsigned octal number (again, an integer).
  692.  
  693.      %s   A character string.
  694.  
  695.      %x   An unsigned hexadecimal number (an integer).
  696.  
  697.      %X   Like %x, but using ABCDEF instead of abcdef.
  698.  
  699.      %%   A single % character; no argument is converted.
  700.  
  701.      There are optional, additional parameters that may lie
  702.      between the % and the control letter:
  703.  
  704.      -    The expression should be left-justified within its
  705.           field.
  706.  
  707.      width
  708.           The field should be padded to this width. If the number
  709.           has a leading zero, then the field will be padded with
  710.           zeros.  Otherwise it is padded with blanks.  This
  711.           applies even to the non-numeric output formats.
  712.  
  713.      .prec
  714.           A number indicating the maximum width of strings or
  715.           digits to the right of the decimal point.
  716.  
  717.      The dynamic width and prec capabilities of the ANSI C
  718.      printf() routines are supported.  A * in place of either the
  719.      width or prec specifications will cause their values to be
  720.      taken from the argument list to printf or sprintf().
  721.  
  722.      Special File Names
  723.  
  724.      When doing I/O redirection from either print or printf into
  725.      a file, or via getline from a file, gawk recognizes certain
  726.      special filenames internally.  These filenames allow access
  727.      to open file descriptors inherited from gawk's parent
  728.      process (usually the shell).  Other special filenames pro-
  729.      vide access information about the running gawk process.  The
  730.      filenames are:
  731.  
  732.      /dev/pid    Reading this file returns the process ID of the
  733.                  current process, in decimal, terminated with a
  734.                  newline.
  735.  
  736.      /dev/ppid   Reading this file returns the parent process ID
  737.                  of the current process, in decimal, terminated
  738.                  with a newline.
  739.  
  740.      /dev/pgrpid Reading this file returns the process group ID
  741.                  of the current process, in decimal, terminated
  742.                  with a newline.
  743.  
  744.      /dev/user   Reading this file returns a single record ter-
  745.                  minated with a newline.  The fields are
  746.                  separated with blanks.  $1 is the value of the
  747.                  getuid(2) system call, $2 is the value of the
  748.                  geteuid(2) system call, $3 is the value of the
  749.                  getgid(2) system call, and $4 is the value of
  750.                  the getegid(2) system call.  If there are any
  751.                  additional fields, they are the group IDs
  752.                  returned by getgroups(2).  Multiple groups may
  753.                  not be supported on all systems.
  754.  
  755.      /dev/stdin  The standard input.
  756.  
  757.      /dev/stdout The standard output.
  758.  
  759.      /dev/stderr The standard error output.
  760.  
  761.      /dev/fd/n   The file associated with the open file descrip-
  762.                  tor n.
  763.  
  764.      These are particularly useful for error messages. For exam-
  765.      ple:
  766.  
  767.           print "You blew it!" > "/dev/stderr"
  768.  
  769.      whereas you would otherwise have to use
  770.  
  771.           print "You blew it!" | "cat 1>&2"
  772.  
  773.      These file names may also be used on the command line to
  774.      name data files.
  775.  
  776.      Numeric Functions
  777.  
  778.      AWK has the following pre-defined arithmetic functions:
  779.  
  780.      atan2(y, x) returns the arctangent of y/x in radians.
  781.  
  782.      cos(expr)   returns the cosine in radians.
  783.  
  784.      exp(expr)   the exponential function.
  785.  
  786.      int(expr)   truncates to integer.
  787.  
  788.      log(expr)   the natural logarithm function.
  789.  
  790.      rand()      returns a random number between 0 and 1.
  791.  
  792.      sin(expr)   returns the sine in radians.
  793.  
  794.      sqrt(expr)  the square root function.
  795.  
  796.      srand(expr) use expr as a new seed for the random number
  797.                  generator. If no expr is provided, the time of
  798.                  day will be used.  The return value is the pre-
  799.                  vious seed for the random number generator.
  800.  
  801.      String Functions
  802.  
  803.      AWK has the following pre-defined string functions:
  804.  
  805.      gsub(r, s, t)           for each substring matching the reg-
  806.                              ular expression r in the string t,
  807.                              substitute the string s, and return
  808.                              the number of substitutions.  If t
  809.                              is not supplied, use $0.
  810.  
  811.      index(s, t)             returns the index of the string t in
  812.                              the string s, or 0 if t is not
  813.                              present.
  814.  
  815.      length(s)               returns the length of the string s,
  816.                              or the length of $0 if s is not sup-
  817.                              plied.
  818.  
  819.      match(s, r)             returns the position in s where the
  820.                              regular expression r occurs, or 0 if
  821.                              r is not present, and sets the
  822.                              values of RSTART and RLENGTH.
  823.  
  824.      split(s, a, r)          splits the string s into the array a
  825.                              on the regular expression r, and
  826.                              returns the number of fields. If r
  827.                              is omitted, FS is used instead.  The
  828.                              array a is cleared first.
  829.  
  830.      sprintf(fmt, expr-list) prints expr-list according to fmt,
  831.                              and returns the resulting string.
  832.  
  833.      sub(r, s, t)            just like gsub(), but only the first
  834.                              matching substring is replaced.
  835.  
  836.      substr(s, i, n)         returns the n-character substring of
  837.                              s starting at i.  If n is omitted,
  838.                              the rest of s is used.
  839.  
  840.      tolower(str)            returns a copy of the string str,
  841.                              with all the upper-case characters
  842.                              in str translated to their
  843.                              corresponding lower-case counter-
  844.                              parts.  Non-alphabetic characters
  845.                              are left unchanged.
  846.  
  847.      toupper(str)            returns a copy of the string str,
  848.                              with all the lower-case characters
  849.                              in str translated to their
  850.                              corresponding upper-case counter-
  851.                              parts.  Non-alphabetic characters
  852.                              are left unchanged.
  853.  
  854.      Time Functions
  855.  
  856.      Since one of the primary uses of AWK programs is processing
  857.      log files that contain time stamp information, gawk provides
  858.      the following two functions for obtaining time stamps and
  859.      formatting them.
  860.  
  861.      systime() returns the current time of day as the number of
  862.                seconds since the Epoch (Midnight UTC, January 1,
  863.                1970 on POSIX systems).
  864.  
  865.      strftime(format, timestamp)
  866.                formats timestamp according to the specification
  867.                in format.  The timestamp should be of the same
  868.                form as returned by systime().  If timestamp is
  869.                missing, the current time of day is used.  See the
  870.                specification for the strftime() function in ANSI
  871.                C for the format conversions that are guaranteed
  872.                to be available.  A public-domain version of
  873.                strftime(3) and a man page for it are shipped with
  874.                gawk; if that version was used to build gawk, then
  875.                all of the conversions described in that man page
  876.                are available to gawk.
  877.  
  878.      String Constants
  879.  
  880.      String constants in AWK are sequences of characters enclosed
  881.      between double quotes ("). Within strings, certain escape
  882.      sequences are recognized, as in C. These are:
  883.  
  884.      \\   A literal backslash.
  885.  
  886.      \a   The ``alert'' character; usually the ASCII BEL charac-
  887.           ter.
  888.  
  889.      \b   backspace.
  890.  
  891.      \f   form-feed.
  892.  
  893.      \n   new line.
  894.  
  895.      \r   carriage return.
  896.  
  897.      \t   horizontal tab.
  898.  
  899.      \v   vertical tab.
  900.  
  901.      \xhex digits
  902.           The character represented by the string of hexadecimal
  903.           digits following the \x.  As in ANSI C, all following
  904.           hexadecimal digits are considered part of the escape
  905.           sequence.  (This feature should tell us something about
  906.           language design by committee.) E.g., "\x1B" is the
  907.           ASCII ESC (escape) character.
  908.  
  909.      \ddd The character represented by the 1-, 2-, or 3-digit
  910.           sequence of octal digits. E.g. "\033" is the ASCII ESC
  911.           (escape) character.
  912.  
  913.      \c   The literal character c.
  914.  
  915.      The escape sequences may also be used inside constant regu-
  916.      lar expressions (e.g., /[ \t\f\n\r\v]/ matches whitespace
  917.      characters).
  918.  
  919. FUNCTIONS
  920.      Functions in AWK are defined as follows:
  921.  
  922.           function name(parameter list) { statements }
  923.  
  924.      Functions are executed when called from within the action
  925.      parts of regular pattern-action statements. Actual
  926.      parameters supplied in the function call are used to instan-
  927.      tiate the formal parameters declared in the function.
  928.      Arrays are passed by reference, other variables are passed
  929.      by value.
  930.  
  931.      Since functions were not originally part of the AWK
  932.      language, the provision for local variables is rather
  933.      clumsy: They are declared as extra parameters in the parame-
  934.      ter list. The convention is to separate local variables from
  935.      real parameters by extra spaces in the parameter list. For
  936.      example:
  937.  
  938.           function  f(p, q,     a, b) { # a & b are local
  939.                          ..... }
  940.  
  941.           /abc/     { ... ; f(1, 2) ; ... }
  942.  
  943.      The left parenthesis in a function call is required to
  944.      immediately follow the function name, without any interven-
  945.      ing white space.  This is to avoid a syntactic ambiguity
  946.      with the concatenation operator.  This restriction does not
  947.      apply to the built-in functions listed above.
  948.  
  949.      Functions may call each other and may be recursive.  Func-
  950.      tion parameters used as local variables are initialized to
  951.      the null string and the number zero upon function invoca-
  952.      tion.
  953.  
  954.      The word func may be used in place of function.
  955.  
  956. EXAMPLES
  957.      Print and sort the login names of all users:
  958.  
  959.           BEGIN     { FS = ":" }
  960.                { print $1 | "sort" }
  961.  
  962.      Count lines in a file:
  963.  
  964.                { nlines++ }
  965.           END  { print nlines }
  966.  
  967.      Precede each line by its number in the file:
  968.  
  969.           { print FNR, $0 }
  970.  
  971.      Concatenate and line number (a variation on a theme):
  972.  
  973.           { print NR, $0 }
  974.  
  975. SEE ALSO
  976.      egrep(1), getpid(2), getppid(2), getpgrp(2), getuid(2),
  977.      geteuid(2), getgid(2), getegid(2), getgroups(2)
  978.  
  979.      The AWK Programming Language, Alfred V. Aho, Brian W. Ker-
  980.      nighan, Peter J. Weinberger, Addison-Wesley, 1988. ISBN 0-
  981.      201-07981-X.
  982.  
  983.      The GAWK Manual, Edition 0.15, published by the Free
  984.      Software Foundation, 1993.
  985.  
  986. POSIX COMPATIBILITY
  987.      A primary goal for gawk is compatibility with the POSIX
  988.      standard, as well as with the latest version of UNIX awk.
  989.      To this end, gawk incorporates the following user visible
  990.      features which are not described in the AWK book, but are
  991.      part of awk in System V Release 4, and are in the POSIX
  992.      standard.
  993.  
  994.      The -v option for assigning variables before program execu-
  995.      tion starts is new.  The book indicates that command line
  996.      variable assignment happens when awk would otherwise open
  997.      the argument as a file, which is after the BEGIN block is
  998.      executed.  However, in earlier implementations, when such an
  999.      assignment appeared before any file names, the assignment
  1000.      would happen before the BEGIN block was run.  Applications
  1001.      came to depend on this ``feature.'' When awk was changed to
  1002.      match its documentation, this option was added to accommo-
  1003.      date applications that depended upon the old behavior.
  1004.      (This feature was agreed upon by both the AT&T and GNU
  1005.      developers.)
  1006.  
  1007.      The -W option for implementation specific features is from
  1008.      the POSIX standard.
  1009.  
  1010.      When processing arguments, gawk uses the special option
  1011.      ``--'' to signal the end of arguments.  In compatibility
  1012.      mode, it will warn about, but otherwise ignore, undefined
  1013.      options.  In normal operation, such arguments are passed on
  1014.      to the AWK program for it to process.
  1015.  
  1016.      The AWK book does not define the return value of srand().
  1017.      The System V Release 4 version of UNIX awk (and the POSIX
  1018.      standard) has it return the seed it was using, to allow
  1019.      keeping track of random number sequences. Therefore srand()
  1020.      in gawk also returns its current seed.
  1021.  
  1022.      Other new features are: The use of multiple -f options (from
  1023.      MKS awk); the ENVIRON array; the \a, and \v escape sequences
  1024.      (done originally in gawk and fed back into AT&T's); the
  1025.      tolower() and toupper() built-in functions (from AT&T); and
  1026.      the ANSI C conversion specifications in printf (done first
  1027.      in AT&T's version).
  1028.  
  1029. GNU EXTENSIONS
  1030.      Gawk has some extensions to POSIX awk.  They are described
  1031.      in this section.  All the extensions described here can be
  1032.      disabled by invoking gawk with the -W compat option.
  1033.  
  1034.      The following features of gawk are not available in POSIX
  1035.      awk.
  1036.  
  1037.           + The \x escape sequence.
  1038.  
  1039.           + The systime() and strftime() functions.
  1040.  
  1041.           + The special file names available for I/O redirection
  1042.             are not recognized.
  1043.  
  1044.           + The ARGIND and ERRNO variables are not special.
  1045.  
  1046.           + The IGNORECASE variable and its side-effects are not
  1047.             available.
  1048.  
  1049.           + The FIELDWIDTHS variable and fixed width field split-
  1050.             ting.
  1051.  
  1052.           + No path search is performed for files named via the
  1053.             -f option.  Therefore the AWKPATH environment vari-
  1054.             able is not special.
  1055.  
  1056.           + The use of next file to abandon processing of the
  1057.             current input file.
  1058.  
  1059.           + The use of delete array to delete the entire contents
  1060.             of an array.
  1061.  
  1062.      The AWK book does not define the return value of the close()
  1063.      function.  Gawk's close() returns the value from fclose(3),
  1064.      or pclose(3), when closing a file or pipe, respectively.
  1065.  
  1066.      When gawk is invoked with the -W compat option, if the fs
  1067.      argument to the -F option is ``t'', then FS will be set to
  1068.      the tab character.  Since this is a rather ugly special
  1069.      case, it is not the default behavior.  This behavior also
  1070.      does not occur if -W posix has been specified.
  1071.  
  1072. HISTORICAL FEATURES
  1073.      There are two features of historical AWK implementations
  1074.      that gawk supports.  First, it is possible to call the
  1075.      length() built-in function not only with no argument, but
  1076.      even without parentheses!  Thus,
  1077.  
  1078.           a = length
  1079.  
  1080.      is the same as either of
  1081.  
  1082.           a = length()
  1083.           a = length($0)
  1084.  
  1085.      This feature is marked as ``deprecated'' in the POSIX stan-
  1086.      dard, and gawk will issue a warning about its use if -W lint
  1087.      is specified on the command line.
  1088.  
  1089.      The other feature is the use of the continue statement out-
  1090.      side the body of a while, for, or do loop.  Traditional AWK
  1091.      implementations have treated such usage as equivalent to the
  1092.      next statement.  Gawk will support this usage if -W posix
  1093.      has not been specified.
  1094.  
  1095. ENVIRONMENT VARIABLES
  1096.      If POSIXLY_CORRECT exists in the environment, then gawk
  1097.      behaves exactly as if --posix had been specified on the com-
  1098.      mand line.  If --lint has been specified, gawk will issue a
  1099.      warning message to this effect.
  1100.  
  1101. BUGS
  1102.      The -F option is not necessary given the command line vari-
  1103.      able assignment feature; it remains only for backwards com-
  1104.      patibility.
  1105.  
  1106.      If your system actually has support for /dev/fd and the
  1107.      associated /dev/stdin, /dev/stdout, and /dev/stderr files,
  1108.      you may get different output from gawk than you would get on
  1109.      a system without those files.  When gawk interprets these
  1110.      files internally, it synchronizes output to the standard
  1111.      output with output to /dev/stdout, while on a system with
  1112.      those files, the output is actually to different open files.
  1113.      Caveat Emptor.
  1114.  
  1115. VERSION INFORMATION
  1116.      This man page documents gawk, version 2.15.
  1117.  
  1118.      Starting with the 2.15 version of gawk, the -c, -V, -C, -a,
  1119.      and -e options of the 2.11 version are no longer recognized.
  1120.      This fact will not even be documented in the manual page for
  1121.      version 2.16.
  1122.  
  1123. AUTHORS
  1124.      The original version of UNIX awk was designed and imple-
  1125.      mented by Alfred Aho, Peter Weinberger, and Brian Kernighan
  1126.      of AT&T Bell Labs. Brian Kernighan continues to maintain and
  1127.      enhance it.
  1128.  
  1129.      Paul Rubin and Jay Fenlason, of the Free Software Founda-
  1130.      tion, wrote gawk, to be compatible with the original version
  1131.      of awk distributed in Seventh Edition UNIX.  John Woods con-
  1132.      tributed a number of bug fixes.  David Trueman, with contri-
  1133.      butions from Arnold Robbins, made gawk compatible with the
  1134.      new version of UNIX awk.
  1135.  
  1136.      The initial DOS port was done by Conrad Kwok and Scott Gar-
  1137.      finkle.  Scott Deifik is the current DOS maintainer.  Pat
  1138.      Rankin did the port to VMS, and Michal Jaegermann did the
  1139.      port to the Atari ST.  The port to OS/2 was done by Kai Uwe
  1140.      Rommel, with contributions and help from Darrel Hankerson.
  1141.  
  1142. ACKNOWLEDGEMENTS
  1143.      Brian Kernighan of Bell Labs provided valuable assistance
  1144.      during testing and debugging.  We thank him.
  1145.  
  1146.